<?php
	mysql_connect("localhost", "root", "%Tg34fv1")or die("cannot connect"); 
	mysql_select_db("wordWaffle")or die("cannot select DB");
	$myKey = mysql_real_escape_string(stripslashes($_GET['sesskey']));
	$result = mysql_query("SELECT * FROM players WHERE sesskey=$myKey");
	if (mysql_num_rows($result) == 1)
	{
		$row=mysql_fetch_assoc(mysql_query("SELECT * FROM gameStatus ORDER BY gameID DESC LIMIT 1"));
		if ($row['status'] == 3) //start a new game
		{
			mysql_query("INSERT INTO gameStatus (status) VALUES (0)");
			$row=mysql_fetch_assoc(mysql_query("SELECT * FROM gameStatus ORDER BY gameID DESC LIMIT 1"));		
		}
		if ($row['status'] != 1) //game is NOT already going on
		{
			mysql_query("UPDATE players SET readyToPlay=1 WHERE sesskey=$myKey");
			echo $row['gameID'];
			return;
		}
	}
	echo 0;
	return;
?>